<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/WEB-INF/view/system/common/head.jsp"%>
<script type="text/javascript">
<!--
$(document).ready(function() {
	setTreeGridList();
});
var groupTypeListJSON = eval('(${groupTypeListJSON})');
function setTreeGridList(){
	$('#groupTreegrid').treegrid({
				title:'用户分组树',
				nowrap:false,fitColumns:true,rownumbers:true,fixed:true,animate:true,singleSelect:false,
				url:'getGroups.action',
				idField:'id',
				treeField:'name',
				frozenColumns:[[
					{title:'名称',field:'name',width:'45%',
						formatter:function(value,rowData){
							var showValue="";
							if(rowData.type==2){
								showValue = '<input name="menuCheckBox" type="checkbox" na="'+rowData.name+'" id="menuCB' + rowData.id + '" value="' + rowData.id + '" /><span style="color:blue">'+value;
							} else {
								showValue='<input name="menuCheckBox" type="checkbox" na="'+rowData.name+'" id="menuCB' + rowData.id + '" value="' + rowData.id + '" /><span style="color:red">'+value;
							}
							var childrens = $('#groupTreegrid').treegrid('getChildren',rowData.id);
							if(childrens.length==0){
								showValue += '('+rowData.rank+')</span>&nbsp;<a href="#"  onclick="showGroupDetailByID('+rowData.id+')">选择组员</a>';
							}else{
								showValue += '</span>';
							}
							return showValue;
					}}
				]]
				,columns:[[{field:'code',title:'编码',width:'10%',hidden:true}
					,{field:'type',title:'类型',width:'20%',formatter:function(val){return groupTypeListJSON[val].name;}}
					,{field:'descr',title:'分组描述',width:'30%'}
				]]
				,tools: [{iconCls: 'icon-reload',handler: function(){
					$("#groupTreegrid").treegrid('reload');unSelectAll();}}]
				,onSelect:function(row){
					var cbs = document.getElementsByName("menuCheckBox");
					for(var i = 0 ;i<cbs.length;i++){
						cbs[i].checked=false;
					}
					
					var selecteds = $('#groupTreegrid').treegrid('getSelections');
					for(var i = 0 ;i<selecteds.length;i++){
						if(selecteds[i].id!=row.id){
							$('#groupTreegrid').treegrid('unselect',selecteds[i].id);
						}
					}
					if(!document.getElementById("menuCB"+row.id).checked){
						document.getElementById("menuCB"+row.id).checked=true;
					}
					//window.frames['groupAddFrame'].document.getElementById('userDataGrid').datagrid('unselectAll');
					window.frames['groupAddFrame'].unSelectAll();
					//$('#groupAddFrame').$('#userDataGrid').datagrid('unselectAll');
					
				},
				onUnselect:function(row){
					if(document.getElementById("menuCB"+row.id).checked){
						document.getElementById("menuCB"+row.id).checked=false;
					}
				},
				onUnselectAll:function(rows){
					for(var i =0;i<rows.length;i++){
						if(document.getElementById("menuCB"+rows[i].id).checked){
							document.getElementById("menuCB"+rows[i].id).checked=false;
						}
					}
				}
			});
}

function showGroupDetailByID(rowId){
	document.getElementById("groupAddFrame").src = "goUserSingleSelect.action?groupId="+rowId;
	//highlightRow
	unSelectAll();
}

function unSelectAll(){
	$('#groupTreegrid').treegrid('unselectAll');
	/*var selecteds = $('#groupTreegrid').treegrid('getSelections');
	for(var i = 0 ;i<selecteds.length;i++){
		$('#groupTreegrid').treegrid('unselect',selecteds[i].id);
	}*/
}
function getSelectGroup() {
	var selectMenuIds = "";
	var cks = document.getElementsByName("menuCheckBox");
	for ( var i = 0; i < cks.length; i++) {
		if (cks[i].checked == true) {
			//selectMenuIds += cks[i].value + ",";
			//单选
			selectMenuIds = cks[i].value ;
			break;
		}
	}
	//alert(selectMenuIds);
	//return selectMenuIds = selectMenuIds.substring(0, selectMenuIds.length - 1);
	return selectMenuIds;
}
function getSelectUser(){
	return window.frames['groupAddFrame'].getSelectUser();
}
/*
var selectedGroupId= getSelectGroup();
if(selectedGroupId!=""){
	alert(selectedGroupId);
}else if(getSelectUser()!=null){
	var selectedUser =getSelectUser();
	alert(selectedUser.code);
	alert(selectedUser.groupid);
}else{
	alert('没选择团队或者用户');
}
 */
//-->
</script>
<body>
	<table width="100%">
		<tr>
			<td width="50%" valign="top">
				<div align="center">
					<table id="groupTreegrid" style="width: 98%"></table>
				</div>
			</td>
			<td width="50%" valign="top">
				<iframe id="groupAddFrame" frameborder="0" name="groupAddFrame"
					style="width: 98%; height: 1000px;" scrolling="yes">
				</iframe>
			</td>
		</tr>
	</table>
</body>
</html>
